package com.bytz.modules.cms.domain.impl.pay;

import com.bytz.modules.cms.common.service.CmsConfigService;
import com.bytz.modules.cms.domain.model.pay.PrepayIn;
import com.bytz.modules.cms.domain.model.pay.PrepayPrepareContext;
import com.bytz.modules.cms.domain.util.PriceComputerUtil;
import com.bytz.modules.cms.order.constant.OrderConstants;
import com.bytz.modules.cms.order.constant.OrderPayConstants;
import com.bytz.modules.cms.order.entity.Order;
import com.bytz.modules.cms.order.entity.OrderItem;
import com.bytz.modules.cms.order.entity.OrderPay;
import com.bytz.modules.cms.order.entity.OrderPayDetail;
import com.bytz.modules.cms.order.model.PayNotifyResult;
import com.bytz.modules.cms.order.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PrepayProcessor extends AbstractPayProcessor<PrepayIn, PrepayPrepareContext> {

    @Override
    protected PrepayPrepareContext createPrepareContext() {
        return new PrepayPrepareContext();
    }

    @Override
    protected void setOrderPayCustomProperties(OrderPay orderPay, PrepayIn payIn) {
        // 没有什么可以赋值的
    }

    @Autowired
    public PrepayProcessor(IOrderService orderService,
                           IOrderItemService orderItemService,
                           IOrderPayService orderPayService,
                           IOrderPayDetailService orderPayDetailService,
                           IOrderOfflinePayService orderOfflinePayService,
                           IOrderEmailService orderEmailService,
                           CmsConfigService configService) {
        super(orderService, orderItemService, orderPayService, orderPayDetailService, orderOfflinePayService, orderEmailService, configService);
    }

    @Override
    protected List<String> getCanPayOrderStatus() {
        return OrderConstants.OrderStatus.CAN_PREPAY_STATUS;
    }

    @Override
    public void calculatePayAmount(PrepayIn payIn, PrepayPrepareContext context) {
        Order order = context.getOrder();
        List<OrderItem> orderItems = context.getOrderItems();

        BigDecimal totalPay = BigDecimal.ZERO;
        BigDecimal prepayRatio = order.getPrepayRatio();
        for (OrderItem orderItem : orderItems) {
            // 计算预付金
            BigDecimal totalTaxPrice = orderItem.getTotalTaxPrice();
            BigDecimal prepayPrice = PriceComputerUtil.saveScale(totalTaxPrice.multiply(prepayRatio));

            // 预付金额保存到订单行上
            orderItem.setPrepayPrice(prepayPrice);
            totalPay = totalPay.add(prepayPrice);
        }

        context.setPayAmount(totalPay);
    }

    @Override
    protected String getPayType() {
        return OrderPayConstants.PayType.PREPAY;
    }

    @Override
    protected String calculateCommodityMessage(PrepayIn payIn, Order order, List<OrderItem> orderItems) {
        return "soldTo【" + order.getDemanderNumber() + "】" + "合同【" + order.getContractNumber() + "】预付款";
    }

    @Override
    protected void prepareNoOnlinePay(PrepayPrepareContext context) {
        super.prepareNoOnlinePay(context);

        OrderPay orderPay = context.getOrderPay();
        List<OrderItem> orderItems = context.getOrderItems();

        // 订单paidAmount更新
        orderService.submitPrepay(orderPay.getOrderId(), orderPay.getTotalPayAmount());

        // 订单行paidAmount更新
        orderItemService.directSubmitPrepay(orderItems);

        // 发送邮件通知om已预付
//        orderEmailService.sendPrepayReminderEmail(context.getOrder());
    }

    @Override
    protected void prepareHasOnlinePay(PrepayPrepareContext context) {
        super.prepareHasOnlinePay(context);

        List<OrderItem> orderItems = context.getOrderItems();

        // 更新行上的预付价格
        orderItemService.updatePrepayPrice(orderItems);
    }

    @Override
    public void processOnlinePaySucceed(PayNotifyResult payNotifyResult, OrderPay orderPay, OrderPayDetail orderPayDetail) {
        // 订单paidAmount更新
        orderService.submitPrepay(orderPay.getOrderId(), orderPay.getTotalPayAmount());

        // 订单行paidAmount更新
        orderItemService.submitPrepay(orderPay.getOrderId());

        // 发送邮件通知om已预付
//        orderEmailService.sendPrepayReminderEmail(orderPay.getOrderId());
    }

    @Override
    public void processOnlinePayFailed(PayNotifyResult payNotifyResult, OrderPay orderPay, OrderPayDetail orderPayDetail) {
        // 什么也不用做
    }
}
